package org.example.util;

import cn.hutool.Hutool;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.GifCaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.digest.MD5;
import org.example.controller.CountController;
import org.example.otherObject.EmailParam;
import org.example.vo.LimitPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Component
public final class HutoolUtil {
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    public String md5EncryptHex(String password){
        String s = DigestUtil.md5Hex(password);
        return s;
    }
    public String md5EncryptHex(String password,String salt){
        String s = md5EncryptHex(password + salt);
        return s;
    }
    public String randomString(){
        String replace = UUID.randomUUID().toString().replace("-", "");
        return replace;
    }
    public String randomString(int length){
        if (length>32) return UUID.randomUUID().toString().replace("-", "");
        String substring = UUID.randomUUID().toString().replace("-", "").substring(0, length);
        return substring;
    }
    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    private String from;
    private static final Context context = new Context();
    @Autowired
    private TemplateEngine templateEngine;
    public int sendMail(EmailParam emailParam,boolean isHtml){
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
        try {
            String title = emailParam.getTitle();
            mimeMessageHelper.setSubject(title == null || title.equals("")?"未知标题":title);
            if (isHtml){
                context.setVariable("username",emailParam.getUsername());
                context.setVariable("url",emailParam.getUrl());
                context.setVariable("id",emailParam.getId());
                context.setVariable("activationCode",emailParam.getActivationCode());
                String process = templateEngine.process(emailParam.getContext(), context);
                mimeMessageHelper.setText(process,isHtml);

            }else {
                mimeMessageHelper.setText(emailParam.getContext(),isHtml);
            }
            mimeMessageHelper.setTo(emailParam.getTo());
            mimeMessageHelper.setFrom(from);
            MimeMessage mimeMessage1 = mimeMessageHelper.getMimeMessage();
            javaMailSender.send(mimeMessage1);
        } catch (MessagingException e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }
    public boolean stringIsEmpty(String param){
        if (param == null || param.equals("") || param.equals(" ")) return true;
        return false;
    }
    public GifCaptcha createGifCaptcha(int width,int height){
        return CaptchaUtil.createGifCaptcha(width, height);
    }
    public GifCaptcha createGifCaptcha(int width,int height,int codeCount){
        return CaptchaUtil.createGifCaptcha(width, height,codeCount);
    }
    public LineCaptcha createLineCaptcha(int width, int height){
        return CaptchaUtil.createLineCaptcha(width, height);
    }
    public LineCaptcha createLineCaptcha(int width, int height,int codeCount,int lineCount){
        return CaptchaUtil.createLineCaptcha(width, height,codeCount,lineCount);
    }

    /**
     *
     * @param currPage 当前页的页码
     * @param size 每页展示多少个
     * @param totalCount 当前查询的总数
     * @return
     */
    public LimitPageVO limitPage(int currPage,int size,int totalCount){
        LimitPageVO result = new LimitPageVO();
        if (totalCount<=0){
            result.setCurrent(1);
            return result;
        }

        if (currPage<=0) currPage = 1;
        int totalPage = totalCount%size==0?totalCount/size:(totalCount/size)+1;
        result.setTotal(totalPage);
        if (currPage>totalPage) currPage = totalPage;
        result.setCurrent(currPage);
        int from = 0;
        int to = 0;
        if (currPage<1){
            from = 1;
            to = to+CommonParam.DEFAULT_INTERVAL_COUNT>totalPage?totalPage:to+CommonParam.DEFAULT_INTERVAL_COUNT;
            result.setFrom(from);
            result.setTo(to);
            return result;
        }
        if (currPage>totalPage){
            to = totalPage;
            from = totalPage-CommonParam.DEFAULT_INTERVAL_COUNT<=0?1:totalPage-CommonParam.DEFAULT_INTERVAL_COUNT;
            result.setFrom(from);
            result.setTo(to);
            return result;
        }
        //在0到totalPage范围内
        from = currPage-CommonParam.DEFAULT_INTERVAL_COUNT<=0?1:currPage-CommonParam.DEFAULT_INTERVAL_COUNT;
        to = currPage+CommonParam.DEFAULT_INTERVAL_COUNT>totalPage?totalPage:currPage+CommonParam.DEFAULT_INTERVAL_COUNT;
        result.setFrom(from);
        result.setTo(to);

        return result;
    }
    public String currDateToString(){
        Date curr = new Date();
        String format = HutoolUtil.format.format(curr);
        return format;
    }
}
